$(document).ready(function() {
	var _userForm = $('#form_user_add');
	var _editForm = $('#form_user_edit');

	$('#submit_form').click(function() {
		var objUserForm = {
			_name: $("input[name='name']", _userForm),
			_rname: $("input[name='realname']", _userForm),
			_group: $("#group"),
			_password: $("input[name='password']", _userForm),
			_password2: $("input[name='password2']", _userForm)

		};
		var pattern = new RegExp("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]");
		for (var i in objUserForm) {
			if (objUserForm[i].val() == '' || objUserForm[i].val() == null || pattern.test(objUserForm[i].val())) {
				objUserForm[i].css({
					'border-color': 'red'
				});
				layer.tips('必填项（请勿输入特殊字符）', objUserForm[i]);
				return false;
			} else {
				objUserForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
			//alert(objUserForm[i].val());
		}

		if (objUserForm._password.val() != objUserForm._password2.val()) {
			alert('两次输入的密码必须一致！');
			return false;
		}

		$.ajax({
			type: "POST",
			url: "/User/add",
			dataType: "JSON",
			async: true,
			data: {
				"name": objUserForm._name.val(),
				"realname": 　objUserForm._rname.val(),
				"group_id": objUserForm._group.val(),
				"password": objUserForm._password.val()

			},
			success: function(data) {
				layer.alert(data.info, function() {
					if (true === data.status) window.location = "/User/list";
				});

			},
			error: function(e) {

				console.log(e);
			}


		});
	});

	//$('.userDelete').click(function(){
	$(document).on('click', '.tableDelete', function() {
		//layer.ready(function(){
		var userId = $(this).parent('td').attr('item_id'),
			thisUrl = $(this).attr('href');
		//官网欢迎页   
		layer.confirm('确认删除', {
			btn: ['确认', '取消'] //按钮
		}, function(index) {

			$.ajax({
				type: "GET",
				url: thisUrl,
				async: true,
				data: {
					//"id" : userId
				},
				success: function(data) {
					if (true === data.status) {
						layer.msg('删除成功！');
						window.location = "/User/list";
					}
				}
			});
			layer.close(index);
			return false;

		}, function(index) {
			layer.close(index);
		});
		return false;

	});

	//$('.userEdit').click(function(){
	$(document).on('click', '.tableEdit', function(e) {
		$('#modal_edit').modal('toggle');
		var _parent = $(this).parent('td').parent('tr.gradeX');
		userId = $(this).parent('td').attr('item_id');
		groupId = $(this).parent('td').attr('group_id');
		objEditForm = {
			_name: $("input[name='name']", _editForm),
			_realName: $("input[name='realname']", _editForm),
			_groupId: $('#groupEdit', _editForm),
			_password: $("input[name='password']", _editForm),
			_password2: $("input[name='password2']", _editForm),
			_password3: $("input[name='password3']", _editForm)

		};
		var tdTable = [];
		$('td', _parent).each(function() {
			tdTable.push($(this).text());
		});

		objEditForm._name.val(tdTable[0]);
		objEditForm._realName.val(tdTable[1]);


		$('option', objEditForm._groupId).each(function() {
			if ($(this).val() == groupId) {
				$(this).attr('selected', true);
			}
		});

		return false;
	});

	$('#submitEdit').click(function() {
		for (var i in objEditForm) {
			if (objEditForm[i].val() == '' || objEditForm[i].val() == null) {
				objEditForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objEditForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
		}
		if (objEditForm._password2.val() != objEditForm._password3.val()) {
			alert('两次输入的密码必须一致！');
			return false;
		}
		$.ajax({
			type: "POST",
			url: "/User/edit/id/" + userId,
			async: true,
			data: {
				"name": objEditForm._name.val(),
				"realname": 　objEditForm._realName.val(),
				"group_id": objEditForm._groupId.val(),
				"password": objEditForm._password.val(),
				"password2": objEditForm._password2.val()

			},
			success: function(data) {
				layer.msg(data.info);
				if (true === data.status) {
					window.location = "/User/list";
				}


			}
		});


	});


	//var nextBtn = $('#page_next');
	$(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];
		$.ajax({
			type: 'POST',
			url: '/User/page/page/' + urlId,
			data: urlId,
			success: function(data) {
				var data = $.parseJSON(data);

				$('#tbody').html('');
				var id = '';


				$.each(data, function(k, v) {
					//console.log('k='+k);
					id = v['user_id'];


					$('#tbody').append($('<tr class="gradeX"><td>' + v['username'] + '</td><td>' + v['realname'] + '</td><td>' + v['group']['name'] + '</td><td>' + v['current_time'] + '</td><td group_id="' + v['group_id'] + '" item_id="' + id + '"><a  class="tableDelete" href="/User/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/User/edit/id/' + id + '">编辑</a> </td></tr>'));

				});

			}

		});
		$.ajax({
			type: 'POST',
			url: '/User/currentPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				console.log(data);
				$('#pages').html(data);
				$('#pages').ready(function() {
					nextBtn = $('#page_next');
				});
			}

		});
		return false;
	});



});